import XLSX from "xlsx";
import export2Excel from "@/util/export2Excel";
import { userpersonalsummaryList } from "@/api/report/report";

export const exportExcel = async (params) => {
  const {
    data: { data },
  } = await userpersonalsummaryList(1, 9999, params);

  let list = [];

  data && data.records.length > 0
    ? data.records.map((item) => {
        let user = {
          学号: item.userCode,
          用户名: item.realName,
          性别: item.sex == 1 ? "男" : "女",
          学校: item.schoolName,
          年级: item.gradeName,
          班级: item.className,
          年度: item.yearly,
          学期: item.term,
          测试项目: item.projectName,
        };
        list.push(user);
      })
    : list.push(data);

  let worksheet1 = XLSX.utils.json_to_sheet(list);

  let headers = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1"];
  headers.forEach((item) => {
    worksheet1[`${item}`].s = {
      font: {
        sz: 14,
        bold: true,
        vertAlign: true,
      },
      fill: {
        bgColor: {
          indexed: 64,
        },
        fgColor: {
          rgb: "C0C0C0",
        },
      },
      alignment: {
        horizontal: "center",
      },
    };
  });

  worksheet1["!cols"] = [
    { wpx: 160 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 180 },
  ];

  export2Excel({
    worksheets: {
      sheet1: worksheet1,
    }, // 导出excel的数据，key表示工作表名，value表示对应工作表的 sheet 数据，支持导出多个工作表
    fileName: "学生预警概况信息", // 导出文件名
    type: "xlsx", // 文件导出类型
  });
};
